<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
    <title>打印条码</title>
    <meta name="decorator" content="default"/>
    <script src="${ctxStatic}/lodop/LodopFuncs.js" type="text/javascript"></script>
    <script type="text/javascript">
        $().ready(function() {
            $("#snPrintForm").validate({
                debug: true,
                submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form
                    printTagOnCreate();
                }
            });
        });
    </script>
</head>
<body>
<ul class="nav nav-tabs">
    <li class="active"><a href="${ctx}/book/purchase/printsn">条码打印</a></li>
    <li><a href="${ctx}/book/purchase/toPrintPlace">书标打印</a></li>
</ul>
<br/>
<form:form id="snPrintForm" modelAttribute="bkPurchaseBatch" action="" method="get" class="form-horizontal">
<div>
    <div class="control-group">
        <label class="control-label">学校名称:</label>

        <div class="controls">
            <input id="shoolName" name="shoolName" type="text" value="${school}"  maxlength="50"  class="required" />
        </div>
    </div>

    <c:if test="${banIds == ''}">
        <div class="control-group">
            <label class="control-label">开始条码号:</label>

            <div class="controls">
                <input id="snBegin" name="snBegin" type="text" maxlength="50" value="${startsn}" class="required digits" />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">结束条码号:</label>

            <div class="controls">
                <input id="snEnd" name="snEnd" type="text" maxlength="50" class="required digits"/>
            </div>
        </div>
    </c:if>
    <c:if test="${banIds != ''}">
        <div class="control-group">
            <label class="control-label">条码列表:</label>
            <div class="controls">
                <input id="banIds" name="banIds" type="text" value="${banIds}" class="required"/>
            </div>
        </div>
    </c:if>
    <div class="form-actions">
        <input id="btnSubmit" class="btn btn-primary" type="submit" onclick="" value="打印"/>&nbsp;
    </div>
</div>
</form:form>
<object id="LODOP" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
    <param name="Border" value="1">
    <param name="Color" value="white">
    <embed id="LODOP_EM" TYPE="application/x-print-lodop" width=0 height=0 border=0 Color="white"
           PLUGINSPAGE="install_lodop.exe">
</object>
<script type="text/javascript">
    function printTagSingle(banid, classcode, count) {
        var place = $("#place_" + count).val();
        printTag(banid, place, classcode);
    }
    function printTagOnCreate() {
        var startsn = $("input[name='snBegin']").val();
        var endsn = $("input[name='snEnd']").val();
        var shoolname = $("input[name='shoolName']").val();
        var banIds = $("input[name='banIds']").val();

        if (checkSn(startsn, endsn, banIds)) {
            printTag(startsn, endsn, shoolname, banIds);
        }
    }
    //对数字进行固定长度的补0处理
    function padLeft(n,num) {
        var l = num.toString().length;
        var str = '';
        if(l < n) {
            while(str.length < n-l){
                str += '0';
            }
            return (str + num);
        }
        return num.toString();
    }
    function checkSn(startsn, endsn, storeIds) {
        if(!storeIds){
            if(startsn.length != endsn.length ){
                alert("开始条形码和结束条形码位数不一致！请重新输入！");
                return false;
            } else if(startsn>endsn) {
                alert("开始条码号不能大于结束条码号！");
                return false;
            }
        }
        return true;
    }
    /**
     *  打印预览
     */
    //声明为全局变量
    var LODOP;
    function printTag(startsn, endsn, shoolname, banIds) {
        LODOP = getLodop(document.getElementById('LODOP'), document.getElementById('LODOP_EM'));
        LODOP.PRINT_INIT("打印");
        LODOP.SET_PRINT_PAGESIZE(1, "149mm", "20mm", "A4");

        var bar = "<div style='background-color:#ffffff;font-size: 11px;'>" +
                "<div style='height:11px;text-align: center;'>" + shoolname + "</div>" +
                "</div>";

        if(!banIds){
            var snLength = startsn.length;

            startsn = parseInt(startsn);
            var length = (endsn - startsn + 1) / 2;
            for (var i = 0; i < length; i++) {
                // 打印条形码
                LODOP.ADD_PRINT_BARCODE(10, 35, 160, 30, "128Auto", padLeft(snLength, startsn));
                LODOP.ADD_PRINT_HTM(40, 25, 170, 11, bar);
                if (startsn + 1 <= endsn) {
                    LODOP.ADD_PRINT_BARCODE(10, 217, 160, 30, "128Auto", padLeft(snLength, startsn + 1));
                    LODOP.ADD_PRINT_HTM(40, 217, 170, 11, bar);
                }
                LODOP.NewPage();
                startsn = startsn + 2;
            }
        }else{
            var banIdList = banIds.split(",");
            var length = banIdList.length;
            startsn = banIdList[0];
            endsn = banIdList[length-1];
            for (var i = 0; i < length; i++) {
                var banId = banIdList[i];
                if(banId != ''){
                    // 打印条形码
                    if(i%2==0){
                        LODOP.ADD_PRINT_BARCODE(10, 35, 160, 30, "128Auto", banIdList[i]);
                        LODOP.ADD_PRINT_HTM(40, 25, 170, 11, bar);
                    }
                    if(i%2==1) {
                        LODOP.ADD_PRINT_BARCODE(10, 217, 160, 30, "128Auto", banIdList[i]);
                        LODOP.ADD_PRINT_HTM(40, 217, 170, 11, bar);
                        LODOP.NewPage();
                    }
                }
            }
        }

        var result = LODOP.PRINT_DESIGN();
        if (result >= 1) {
            $.ajax({
                type: "POST",
                url: "${ctx}/book/purchase/savePrint",
                data: {startsn: startsn, endsn: endsn, school: shoolname},
                dataType: "json",
                success: function (data) {
                }
            });
        }
    }
</script>
</body>
</html>